/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.oai.metadataRepository.sysnoSignatureMapping;

import cz.mzk.oai.metadataRepository.repository.Identifier;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 *
 * @author Martin Řehánek (rehan at mzk.cz)
 */
public class MappingDocumentBuilder {

    private List<Mapping> mappings;

    public MappingDocumentBuilder(List<Mapping> mappings) {
        this.mappings = mappings;
    }

    public Document getDocument() {
        Document doc = DocumentHelper.createDocument();
        Element mappingsEl = doc.addElement("mappings");
        for (Mapping mapping : mappings) {
            addMapping(mappingsEl, mapping);
        }
        return doc;
    }

    private void addMapping(Element mappingsEl, Mapping mapping) {
        Element recordEl = mappingsEl.addElement("record");
        Identifier id = mapping.getId();
        addId(recordEl, id);
        String signature = mapping.getSignature();
        addSignature(recordEl, signature);
    }

    private void addId(Element recordEl, Identifier id) {
        Element idEl = recordEl.addElement("id");
        Element baseEl = idEl.addElement("base");
        baseEl.addText(id.getBase().toString());
        Element sysnoEl = idEl.addElement("sysno");
        sysnoEl.addText(id.getSysno());
    }

    private void addSignature(Element recordEl, String signature) {
        Element signatureEl = recordEl.addElement("signature");
        signatureEl.addText(signature);
    }
}
